/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年7月15日         Root.Lu
 */

package com.coocaa.x.service.litedownloader.db;

import android.database.Cursor;

import com.coocaa.x.framework.utils.ICache;
import com.coocaa.x.service.litedownloader.data.DownloadTask;
import com.skyworth.framework.skysdk.android.SkyDBUtil.QueryHandler;
import com.skyworth.framework.skysdk.util.SkyDB;

import java.util.ArrayList;
import java.util.List;

public class DownloaderTable extends DBTable implements ICache<String, DownloadTask> {
    private static final String defaultTableName = "tb_download_task_list";
    private static final String[][] defaultTableColumnsAndTypes = {
            {"id", "INTEGER PRIMARY KEY"}, {"task_id", SkyDB.FILED_TYPE_STRING},
            {"task_main", SkyDB.FILED_TYPE_STRING}};

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    @Override
    public String getTableName() {
        // TODO Auto-generated method stub
        return defaultTableName;
    }

    @Override
    public String[][] getColumnsAndTypes() {
        // TODO Auto-generated method stub
        return defaultTableColumnsAndTypes;
    }

    @Override
    public void add(String key, DownloadTask value) {
        // TODO Auto-generated method stub
        synchronized (this) {
            String task_id = key;
            String task_main = value.toString();
            String[] values = {null, task_id, task_main};
            dbUtil.insert(defaultTableName, values);
        }
    }

    @Override
    public void update(String key, DownloadTask value) {
        // TODO Auto-generated method stub
        synchronized (this) {
            String sql = "UPDATE " + defaultTableName + " SET task_main='" + value.toString()
                    + "' where task_id='" + key + "'";
            try {
                dbUtil.exec(sql);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public DownloadTask get(String key) {
        // TODO Auto-generated method stub
        String sql = "SELECT * FROM " + defaultTableName + " WHERE task_id='" + key + "'";
        DownloadTask ret = (DownloadTask) dbUtil.query(sql, null, new QueryHandler() {
            @Override
            public Object onResult(Cursor cursor, Object obj) {
                DownloadTask ret = null;
                boolean hasData = cursor.moveToFirst();
                if (hasData) {
                    String task_main = cursor.getString(cursor.getColumnIndex("task_main"));
                    ret = DownloadTask.parseJObject(task_main, DownloadTask.class);
                }
                return ret;
            }
        });
        return ret;
    }

    @Override
    public void remove(String key) {
        // TODO Auto-generated method stub
        synchronized (this) {
            String sql = "DELETE FROM " + defaultTableName + " WHERE task_id='" + key + "'";
            dbUtil.exec(sql);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String> keys() {
        // TODO Auto-generated method stub
        List<String> ret = null;
        String sql = "SELECT * FROM " + defaultTableName;
        ret = (List<String>) dbUtil.query(sql, null, new QueryHandler() {
            @Override
            public Object onResult(Cursor cursor, Object obj) {
                List<String> list = new ArrayList<String>();
                boolean hasData = cursor.moveToFirst();
                while (hasData) {
                    String key = cursor.getString(cursor.getColumnIndex("task_id"));
                    list.add(key);
                    hasData = cursor.moveToNext();
                }
                return list;
            }
        });
        if (ret == null)
            ret = new ArrayList<String>();
        return ret;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<DownloadTask> values() {
        // TODO Auto-generated method stub
        List<DownloadTask> ret = null;
        String sql = "SELECT * FROM " + defaultTableName;
        ret = (List<DownloadTask>) dbUtil.query(sql, null, new QueryHandler() {
            @Override
            public Object onResult(Cursor cursor, Object obj) {
                List<DownloadTask> list = new ArrayList<DownloadTask>();
                boolean hasData = cursor.moveToFirst();
                while (hasData) {
                    String task_main = cursor.getString(cursor.getColumnIndex("task_main"));
                    DownloadTask task = DownloadTask.parseJObject(task_main, DownloadTask.class);
                    list.add(task);
                    hasData = cursor.moveToNext();
                }
                return list;
            }
        });
        if (ret == null)
            ret = new ArrayList<DownloadTask>();
        return ret;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        String sql = "SELECT * FROM " + defaultTableName;
        int ret = (Integer) dbUtil.query(sql, null, new QueryHandler() {
            @Override
            public Object onResult(Cursor cursor, Object obj) {
                int count = 0;
                boolean hasData = cursor.moveToFirst();
                while (hasData) {
                    count++;
                    hasData = cursor.moveToNext();
                }
                return count;
            }
        });
        return ret;
    }

    @Override
    public boolean containsKey(String key) {
        // TODO Auto-generated method stub
        String sql = "SELECT * FROM " + defaultTableName + " WHERE task_id='" + key + "'";
        boolean ret = (Boolean) dbUtil.query(sql, null, new QueryHandler() {
            @Override
            public Object onResult(Cursor cursor, Object obj) {
                return cursor.moveToFirst();
            }
        });
        return ret;
    }

    @Override
    public boolean containsValue(DownloadTask value) {
        // TODO Auto-generated method stub
        return containsKey(value.ID);
    }
}
